<?

require_once('db.php');
require_once('config.inc.php');


/* Reused Functions */
function AuthLnk($aid,$name){
	if($name==''){
		$name = 'UNKNOWN';
	}
	$sHTML ='<a href="#nogo" onclick="xajax_showAuthor(\''.$aid.'\');" class="author">'.trim($name).'</a>';
	return $sHTML;
}

function TransSite($key){
	global $sites;
	$sHTML = $sites[$key]['label'];
	
	return $sHTML;
}

function changeStat($id,$stat){
	global $db;

	$objResponse = new xajaxResponse();
	$objResponse-> addRemove("sA".$id);
	$objResponse-> addRemove("sB".$id);

	if($stat=='R' || $stat=='C'){ $sq2 = ',chapter_at=chapters'; }
	
	$sql = "UPDATE fic_listing SET status='".$stat."' ".$sq2." where id='".$id."'";
	$db->Execute($sql);
	
	return $objResponse;
}	

function statusMod($id,$stat,$active_stat){

	$sHTML .= '<select onchange="xajax_changeStat('.$id.',this.value);"';
		if(!$active_stat){ $sHTML .= ' , disabled '; }
	$sHTML .= '>';
			$sHTML .= '<option value="'.$stat.'"></option>';

			$sHTML .= '<option value="N"';
				if($stat=='N'){ $sHTML .= ' , selected '; }
			$sHTML .= '>New</option>';

			$sHTML .= '<option value="U"';
				if($stat=='U'){ $sHTML .= ' , selected '; }
			$sHTML .= '>Updated</option>';

			$sHTML .= '<option value="R"';
				if($stat=='R'){ $sHTML .= ' , selected '; }
			$sHTML .= '>Read</option>';

			$sHTML .= '<option value="C"';
				if($stat=='C'){ $sHTML .= ' , selected '; }
			$sHTML .= '>Complete</option>';

			$sHTML .= '<option value="?"';
				if($stat=='?'){ $sHTML .= ' , selected '; }
			$sHTML .= '>Unfiled</option>';

			$sHTML .= '<option value="I"';
				if($stat=='I'){ $sHTML .= ' , selected '; }
			$sHTML .= '>Ignore</option>';

	$sHTML .= '</select>';

	return $sHTML;
}

function ChapterNav($id,$chapters,$chapter_at,$chap_sel=false){

$sHTML .= '<td class="controlsr1a">';
if($chap_sel && $chapter_at>1){
	$sHTML .= '<a href="#nogo" onclick="xajax_showStory('.$id.',1);"><img src="icons/book.png" border="0"></a>';
}
$sHTML .= '</td><td class="controlsr1b">';
if($chap_sel && $chapter_at>1){
	$sHTML .= '<a href="#nogo" onclick="xajax_showStory('.$id.','.($chapter_at-1).');"><img src="icons/book_previous.png" border="0"></a>';
}
$sHTML .= '</td><td class="controlsr1d" >';
		$sHTML .=    $chapter_at.'/'.$chapters;
		$sHTML .= '</td>';
$sHTML .= '<td class="controlsr1b">';
if($chap_sel && $chapter_at<$chapters){
	$sHTML .= '<a href="#nogo" onclick="xajax_showStory('.$id.','.($chapter_at+1).');"><img src="icons/book_next.png" border="0"></a>';
}
$sHTML .= '</td>';
	return $sHTML;
}


function drawFlag($id,$type,$flag){

	if($flag==''){ $flag = '-'; }

	$flagsA['F']['-']= '<a href="#nogo" onclick="xajax_flagFav('.$id.',\'X\');"><img src="icons/award_star_silver_1.png" border="0" align="center"></a>';

	$flagsA['F']['X']= '<a href="#nogo" onclick="xajax_flagFav('.$id.',\'\');"><img src="icons/award_star_gold_1.png" border="0" align="center"></a>';

	$flagsA['P']['-']= '<a href="#nogo" onclick="xajax_flagPWP('.$id.',\'X\');"><img src="icons/picture_empty.png" border="0" align="center"></a>';

	$flagsA['P']['X']= '<a href="#nogo" onclick="xajax_flagPWP('.$id.',\'\');"><img src="icons/picture.png" border="0" align="center"></a>';

	return $flagsA[$type][$flag];

}


function flagFav($id,$flag){
	global $db;
	$sql3 = "UPDATE fic_listing SET favs='".$flag."' WHERE id='".$id."'";
	$db->Execute($sql3);	
	$objResponse = new xajaxResponse();
	if($flag=='X'){$flagNew='';}else{$flagNew='X';}

	$objResponse->addAssign($id.'Fav',"innerHTML",drawFlag($id,'F',$flag));
	return $objResponse;
}

function flagPWP($id,$flag){
	global $db;
	$sql3 = "UPDATE fic_listing SET pwp='".$flag."' WHERE id='".$id."'";
	$db->Execute($sql3);	
	$objResponse = new xajaxResponse();
	if($flag=='X'){$flagNew='';}else{$flagNew='X';}
	$objResponse->addAssign($id.'PWP',"innerHTML",drawFlag($id,'P',$flag));
	return $objResponse;
}


/* Basic Display Function */
function drawEntry($rs,$chap_sel,$active_stat){
		$flg1 = '<a href="#nogo" onclick="xajax_flagFav('.$rs->fields('id').',\''.$rs->Fields('favs').'\');"><img src="icons/award_star_silver_1.png" border="0" align="center"></a>';
		$flg2 = '<a href="#nogo" onclick="xajax_flagPWP('.$rs->fields('id').',\''.$rs->Fields('pwp').'\');"><img src="icons/picture_empty.png" border="0" align="center"></a>';

	
	$sHTML .=    '<tr id="sA'.$rs->Fields('id').'" class="storylist">
		<td class="tdpad"></td>		
		<td class="flags" id="'.$rs->Fields('id').'Fav">'.drawFlag($rs->fields('id'),'F',$rs->fields('favs')).'</td>
		<td class="flags" id="'.$rs->Fields('id').'PWP">'.drawFlag($rs->fields('id'),'P',$rs->fields('pwp')).'</td>
		<td class="title"><a href="#nogo" onclick="xajax_showStory(\''.$rs->Fields('id').'\');">'.$rs->Fields('story_title').'</a></td>
		<td class="author">'.AuthLnk($rs->Fields('linkid'),$rs->Fields('author_name')).'</td>
		<td class="site">'.TransSite($rs->Fields('sitekey')).'</td>
		<td>'.$rs->Fields('updated').'</td>
		';

		$sHTML .= ChapterNav($rs->Fields('id'),$rs->Fields('chapters'),$rs->Fields('chapter_at'),$chap_sel);
	$sHTML .=    '</tr>';
	$sHTML .=    '<tr id="sB'.$rs->Fields('id').'" class="storylist">
	<td class="r2"></td>
	<td class="r2"></td>
	<td class="r2"></td>
		<td colspan="4" class="summary">'.$rs->Fields('summary').'</td>
		<td  colspan="4" valign="top" class="controlsr2">'.StatusMod($rs->Fields('id'),$rs->Fields('status'),$active_stat).'</td>
	    </tr>';

	return $sHTML;
}

function drawListing($rs,$list_table,$active_stat){
	
	$sHTML .='<table  id="listtable" cellspacing="0">';
	$sHTML .=    '<tr class="storylist">
	<td class="r2"></td>
	<td class="r2"></td>
	<td class="r2"></td>
		<td colspan="4" class="summary"><b>'.$list_table.'</b></td>
<td  colspan="4" valign="top" class="controlsr2"></td>	    </tr>';

	$lst =0;
	$linv=$rs->NumRows();
	for($i=$lst;$i<$linv;$i++){
		$sHTML.=drawEntry($rs,false,$active_stat);
		$rs->MoveNext(); 
	} 
	$sHTML .= '<tr><td class="r3" colspan="11"></td></tr>';

	$sHTML .=    '</table>';
	
	return $sHTML;
}


/* Listing Display Functions */
function showCurrent(){
	global $db;
	$objResponse = new xajaxResponse();
	$sql = LISTINGSQL." WHERE  status IN ('N','U') order by updated desc, inserted desc LIMIT 0,150";

	$rs = $db->Execute($sql);
	if(!$rs->EOF){
		$objResponse->addAssign("content","innerHTML",drawListing($rs,'Current Fiction',true));
	}
	return $objResponse;
}

function showFinished(){
	global $db;
	$objResponse = new xajaxResponse();
	$sql = LISTINGSQL." WHERE status='C' order by  bookmark desc LIMIT 0,150";
	$rs = $db->Execute($sql);
	
	$objResponse->addAssign("content","innerHTML",drawListing($rs,'Finished Stories',false));
	
	return $objResponse;
}

function showBookmarks(){
	global $db;
	$objResponse = new xajaxResponse();
		$sql = LISTINGSQL." WHERE  (
					status IN ('U','R','C')
					or 
					(status='N' and chapter_at!='1') 
				  ) 
			and bookmark!='0000-00-00 00:00:00' 
			and (chapters!=chapter_at ) 
			order by  bookmark desc, updated desc
			LIMIT 0,150";
	$rs = $db->Execute($sql);

	$objResponse->addAssign("content","innerHTML",drawListing($rs,'Recently Read',false));

	return $objResponse;
}

function showMostRecent(){
	global $db;
		$sql = LISTINGSQL." WHERE  (
					status IN ('U','R','C')
					or 
					(status='N' and chapter_at!='1') 
				  ) 
			and bookmark!='0000-00-00 00:00:00' 
			and (chapters!=chapter_at ) 
			order by  bookmark desc, updated desc
			LIMIT 0,1";
	$rs = $db->Execute($sql);

	$objResponse = showStory($rs->Fields('id'));
	return $objResponse;
}

function showFavs(){
	global $db;
	$objResponse = new xajaxResponse();
		$sql = LISTINGSQL." WHERE  favs='X'
			order by updated desc";
			//$sql .=" LIMIT 0,150";
		$rs = $db->Execute($sql);

	$objResponse->addAssign("content","innerHTML",drawListing($rs,'Favorite Fics',false));

	return $objResponse;
}

function showPWP($status=0){
	global $db;
	$objResponse = new xajaxResponse();
		$sql = LISTINGSQL." WHERE  pwp='X'
			order by updated desc";
			//$sql .=" LIMIT 0,150";
		$rs = $db->Execute($sql);

	
	$objResponse->addAssign("content","innerHTML",drawListing($rs,'PWP Fiction',false));

	return $objResponse;
}

function showNew($key,$show = 0){
	global $db;
	$objResponse = new xajaxResponse();

		$sql = LISTINGSQL." WHERE  fic_listing.sitekey='".$key."' and  status IN ('?') order by  updated desc,inserted desc, story_title LIMIT 0,350";
		$rs = $db->Execute($sql);


	$objResponse->addAssign("content","innerHTML",drawListing($rs,'New Fiction '.TransSite($rs->Fields('sitekey')),true));

	return $objResponse;
}



/* Author Listing */
function showAuthor($aid){
	global $db, $sites;
	$objResponse = new xajaxResponse();

		$sql = LISTINGSQL." WHERE  fic_authors.linkid='".$aid."' and status!='I' order by updated desc";

		$rs = $db->Execute($sql);
		

		$asql = "SELECT author_name,sitekey,author_id FROM fic_authors WHERE linkid='".$aid."'";

		$ars = $db->Execute($asql);

		if(!$ars->EOF){

			$alist = "";

			while(!$ars->EOF){

				$alist .= '(<a href="admin.php?auth='.$sites[$ars->Fields('sitekey')]['upd'].'&aid='.$ars->Fields('author_id').'" target="_new">'.$sites[$ars->Fields('sitekey')]['label']." - ".$ars->Fields('author_name')."</a>)&nbsp;&nbsp;";

				$ars->MoveNext();

			}

		}

		
	$objResponse->addAssign("content","innerHTML",drawListing($rs,'Author Listing: '.$alist,true));

	return $objResponse;
}


/* Story Display Function */


function showStory($id,$ch=''){
	global $db, $sites;
	$objResponse = new xajaxResponse();
	
	$sql1 = "UPDATE fic_listing SET bookmark=NOW() WHERE id='".$id."' ";
	$db->Execute($sql1);
if($ch!=''){
	$sql2 = "UPDATE fic_listing SET chapter_at='".$ch."' WHERE  id='".$id."'";
	$db->Execute($sql2);
	$sql3 = "UPDATE fic_listing SET status='R' WHERE id='".$id."' AND chapter_at=chapters and chapter_at!='1' and status!='C'";
	$db->Execute($sql3);	
}
		$sql = LISTINGSQL." WHERE  fic_listing.id='".$id."'";
		$rs = $db->Execute($sql);

	$sHTML ='<table id="listtable" cellspacing="0">';
		$sHTML.=drawEntry($rs,true,true);


	$sHTML .= '<tr><td class="r3" colspan="11">';

	$sHTML .= '<iframe id="myframe" src="blank.php" marginheight="8" marginwidth="8" frameborder="0"></iframe>';

	$sHTML .= '</td></tr>';
	$sHTML .= '</table>';

	$sid= $rs->fields['story_id'];
	$chat = $rs->fields['chapter_at'];
	$surl = $sites[$rs->Fields('sitekey')]['surl'];
	eval("\$url=\"$surl\";");

	$objResponse->addAssign("content","innerHTML",$sHTML);
	$objResponse->addAssign("myframe", "src", $url);

	return $objResponse;
}


/* Admin */
function showAdmin(){
	global $db, $sites;
$r = $db->Execute("SELECT sitekey, max( inserted )
FROM `fic_listing`
GROUP BY sitekey");
$upd = $r->GetAssoc();
	$objResponse = new xajaxResponse();
$sHTML = '<table id="listtable" cellspacing="0">

<tr>
	<td class="r2"></td>
	<td class="r2"><img src="icons/wrench.png" border="0"></td>
	<td class="summary" colspan="4"><b><i>Administrative Tools</i></b></td>
<td class="summary" colspan="1" id="dbfld">';
/*<a href="#nogo" onclick="xajax_DbBackup();"><img src="icons/database_save.png" border="0"></a> <a href="databackup.sql" target="_new">Backup File ('.date("Y-m-d H:i:s",@filemtime('databackup.sql')).')</a>*/
$sHTML .= '</td>
	<td colspan="4" class="r2">&nbsp;</td>
</tr>

<tr>
	<td class="tdpad"></td>
	<td class="flags"><img src="icons/database_refresh.png" border="0"></td>
	<td class="r1" colspan="9"><b>Update Site Data</b></td>
</tr>

';
$sitelist = $sites;
$numr =count($sitelist);
$rsp=' rowspan="'.$numr.'" ><iframe src="blank.php" name="admif"></iframe>';
while(list($k,$inf)=each($sitelist)){
/*
<td><a href="admin.php?upd=<?=$inf['upd'];?>" class="btAlink"><?=$inf['label'];?></a></td>
*/

$sHTML .='<tr>
	<td class="tdpad"></td>
	<td class="flags"></td>
	<td class="flags"><a href="admin.php?upd='.$inf['upd'].'" target="admif"><img src="icons/arrow_refresh.png" border="0"></a></td>	<td class="site" >'.$inf['label'].'</td>
	<td class="author" colspan="3" >'.$upd[$k].'</td>


	<td class="r1"><form action="admin.php" method="GET" target="admif">

	<input type="hidden" name="auth" value="'.$inf['upd'].'"><input type="text" size="4" name="aid"><input type="image" src="icons/add.png" border="0"></form></td>


	<td class="r1" colspan="2" '.$rsp.'</td>

</tr>';
if($rsp!='>'){$rsp='>';}
}

$sHTML.='
<tr>
	<td class="r2"></td>
	<td class="r2"></td>
	<td class="summary" colspan="4"></td>
	<td class="summary" colspan="1"></td>
	<td colspan="4" class="r2">&nbsp;</td>
</tr>

<tr>
	<td class="tdpad"></td>

	<td class="flags"><img src="icons/link.png" border="0"></td>
	<td class="r1" colspan="9"><b>Link Author Profiles</b></td>
</tr>
<tr>
	<td class="tdpad"></td>
	<td class="flags"></td>
	<td class="flags"><a href="?admin"><img src="icons/link_add.png" border="0"></a></td>
	<td class="site" > </td>
	<td class="author" > </td>

	<td class="r1" colspan="3"></td>
</tr>
<tr>
	<td class="r2"></td>
	<td class="r2"></td>
	<td class="summary" colspan="4"></td>
	<td class="summary" colspan="1"></td>
	<td colspan="4" class="r2">&nbsp;</td>
</tr>
<tr>
	<td class="tdpad"></td>

	<td class="flags"><img src="icons/asterisk_yellow.png" border="0"></td>
	<td class="r1" colspan="9"><b>About Webbook 4</b></td>
</tr>



<tr>
	<td class="tdpad"></td>
	<td class="r3" colspan="10" valign="top">
	Primary Coding By: Lathrop Preston<br/>

	Additional Coding By: Robert Preston<br/>
	Icon Set By: Mark James <a href="http://www.famfamfam.com/lab/icons/silk/" target="_new">http://www.famfamfam.com/</a> "Silk Icons"<br/>
	AJAX Library: xajax <a href="http://www.xajaxproject.org/" target="_new">http://www.xajaxproject.org/</a><br/>
	</td>
</tr>

</table>';
	$objResponse->addAssign("content","innerHTML",$sHTML);
	return $objResponse;
}



/* Story Search */
function showSearch(){
	global $db,$sites;
	$objResponse = new xajaxResponse();
$sHTML ='<table id="listtable" cellspacing="0">
<tr>
	<td class="r2"></td>
	<td class="r2"></td>
	<td class="r2"><img src="icons/find.png" border="0"></td>
	<td class="summary" colspan="4"><b><i>Search Webbook</i></b></td>	
	<td colspan="4" class="r2">
	</td>
</tr>
<tr>
	<td class="r2"></td>
	<td class="r2"></td>
	<td class="r2"></td>
	<td class="r2" colspan="4">
<form action="#" id="searchFRM" onsubmit="return false;">
	<table cellspacing="0" cellpadding="1" border="0">
	<tr>
	<td align="right"><b>Title</b></td><td><input type="text" name="titlesearch"></td>
	<td align="right"><b>Author</b></td><td><input type="text" name="authorsearch"></td>
	<td align="right"><b>Summary</b></td><td><input type="text" name="summarysearch"></td>
	</tr>
	<tr>
	<td align="right"><b>Site</b></td><td colspan="5">
		<table cellspacing="0" cellpadding="0" border="0">
		<tr>';

	$sitelist = $sites;
	while(list($k,$inf)=each($sitelist)){
		$sHTML.='<td width="85"><input type="radio" name="sitekey" value="'.$k.'"> '.$inf['label'].'</td>';
	}

	$sHTML.='</tr>
		</table>
	</td>
	</tr>
	<tr>
	<td align="right"><b>Status</b></td><td colspan="5">
		<table cellspacing="0" cellpadding="0" border="0">
		<tr>
		<td width="85"><input type="checkbox" name="status[]" value="N" > New</td> 
		<td width="85"><input type="checkbox" name="status[]" value="U" > Updated</td> 
		<td width="85"><input type="checkbox" name="status[]" value="R" > Read</td>
		<td width="85"><input type="checkbox" name="status[]" value="C" > Complete</td> 
		<td width="85"><input type="checkbox" name="status[]" value="?" > Unfiled</td>
		</tr>
		</table>
	</tr>
	<tr>
	<td align="right"><b>Category</b></td><td colspan="5">
		<table cellspacing="0" cellpadding="0" border="0">
		<tr>';
$rsC = $db->Execute("Select * from fic_metanames where stat!='I' order by cat_label");
while(!$rsC->EOF){
	$sel1 .= '<option value="'.$rsC->Fields('universe_id').'">'.$rsC->Fields('cat_label').'</option>';
	$sel2 .= '<option value="'.$rsC->Fields('universe_id').'">'.$rsC->Fields('cat_label').'</option>';

	$rsC->MoveNext();
}
	$sHTML.='<td width="170" ><select name="cat1"><option value=""></option>'.$sel1.'</select></td>';
	//$sHTML.='<td width="170" ><select name="cat2"><option value=""></option>'.$sel2.'</select></td>';


	$sHTML.='</tr>
		</table>
	</td>
	</tr>
	<tr>
	<td align="right"></td><td colspan="5">
	<input  id="post" type="submit" value=" Search " onclick="xajax_showRes(xajax.getFormValues(\'searchFRM\'));" \>
	</td>
	</tr>
	</table>
</form>
	</td>
	<td class="r2" colspan="4"></td>
</tr>
<tr>

	<td class="r3" colspan="12" valign="top" id="searchres">Results!</td>
</tr>
</table>';
	$objResponse->addAssign("content","innerHTML",$sHTML);
	return $objResponse;

}



function showRes($dta){

	global $db;
	$objResponse = new xajaxResponse();
		
				$sql = RLISTINGSQL." WHERE  status NOT IN ('I','D') ";
				
	$doit=false;
				if(is_array($dta['status'])){
					while(list($key,$status)=each($dta['status'])){
					if($statsql!=''){$statsql.=",";}
						$statsql .= "'".$status."'";
					}
					$sql .= " and status IN (".$statsql.") ";
					$doit=true;
				}
				if($dta['sitekey']!=''){
					$sql .= " and fic_listing.sitekey = '".$dta['sitekey']."' ";
					$doit=true;
				}
				if($dta['titlesearch']!=''){
					$sql .= " and (story_title like '%".$dta['titlesearch']."%')";
					$doit=true;
				}
				if($dta['summarysearch']!=''){
					$sql .= " and (summary like '%".$dta['summarysearch']."%')";
					$doit=true;
				}
				if($dta['authorsearch']!=''){
					$sql .= " and (author_name like '%".$dta['authorsearch']."%')";
					$doit=true;
				}
				if($dta['cat1']!='' || $dta['cat2']!=''){
					$sql.= " and (";					
					if($dta['cat1']!=''){
						$sql.= " universe_id='".$dta['cat1']."' ";
					}
					if($dta['cat2']!=''){
						if($dta['cat1']!=''){ $sql.= " and "; }
						$sql.= " universe_id='".$dta['cat1']."' ";
					}
					$sql .= ")";
					$doit=true;
				}

				$sql .= " order by updated desc, story_title LIMIT 0,100";
	
		$sql = $sq.$sql;
//$doit=false;
//$objResponse->addAlert($sql);

if($doit){
		$rs = $db->Execute($sql);

	//$sHTML.=$sql;
	//$sHTML.="<br>".$rs->NumRows();
	if($rs===false){ $sHTML = $db->ErrorMsg()." ".$sql; }else{
		for($i=0;$i<$rs->NumRows();$i++){
			$sHTML.=drawEntry($rs,false,true);
			$rs->MoveNext(); 
		} 
	}
	$objResponse->addAssign("searchres","innerHTML",$sHTML);
}
	return $objResponse;

}



require("dogear.common.php");
$xajax->processRequests();
?>
